package cn.loveq.android;

import cn.loveq.android.widget.FlingGallery;
import cn.loveq.android.widget.InteractiveGalleryViewItem;
import cn.loveq.android.widget.RadioGalleryViewItem;
import cn.loveq.android.widget.WeiboGalleryViewItem;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;

/**
 * 
 * 
 * 项目名称：loveq4Weibo 类名称：MainActivity 类描述：主界面 创建人：李伟豪 创建时间：2011-12-5 下午03:16:40
 * 
 * @version
 * 
 */
public class MainActivity extends Activity {
	private final int color_red = Color.argb(100, 200, 0, 0);
	private final int color_green = Color.argb(100, 0, 200, 0);
	private final int color_blue = Color.argb(100, 0, 0, 200);

	private final String[] mLabelArray = { "Raido界面", "互动空间" };
	private final int[] mColorArray = { color_red, color_green, color_blue };

	private FlingGallery mGallery;

	// private CheckBox mCheckBox;

//	@Override
//	public boolean onTouchEvent(MotionEvent event) {
//		return mGallery.onGalleryTouchEvent(event);
//	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);
		mGallery = new FlingGallery(this);
		mGallery.setBackgroundResource(R.drawable.bg);
		mGallery.setPaddingWidth(5);
		mGallery.setIsGalleryCircular(false);
		mGallery.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, mLabelArray) {
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				Log.d(Constants.TAG, "count=" + position);
				// if (convertView != null && convertView instanceof
				// GalleryViewItem)
				// {
				// GalleryViewItem galleryView = (GalleryViewItem) convertView;
				//
				// galleryView.mEdit1.setText("");
				// galleryView.mText1.setText(mLabelArray[position]);
				// galleryView.mText1.setBackgroundColor(mColorArray[position]);
				// galleryView.mText2.setText(mLabelArray[position]);
				// galleryView.mText2.setBackgroundColor(mColorArray[position]);
				//
				// Log.d("111", "count="+position);
				//
				// return galleryView;
				//
				// }
				switch (position) {
				case 0:
					/*return new WeiboGalleryViewItem(getApplicationContext(),
							position);*/
					return new RadioGalleryViewItem(MainActivity.this, position);
				case 1:
					return new InteractiveGalleryViewItem(getApplication(), position);
				case 2:
				default:
					break;
				}
				return new WeiboGalleryViewItem(getApplicationContext(), position);
			}
		});

		LinearLayout layout = new LinearLayout(getApplicationContext());
		layout.setOrientation(LinearLayout.VERTICAL);

		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

		layoutParams.setMargins(10, 10, 10, 10);
		layoutParams.weight = 1.0f;

		layout.addView(mGallery, layoutParams);

		// mCheckBox = new CheckBox(getApplicationContext());
		// mCheckBox.setText("Gallery is Circular");
		// mCheckBox.setPadding(50, 10, 0, 10);
		// mCheckBox.setTextSize(30);
		// mCheckBox.setChecked(true);
		// mCheckBox.setOnClickListener(new OnClickListener()
		// {
		// @Override
		// public void onClick(View view)
		// {
		// mGallery.setIsGalleryCircular(mCheckBox.isChecked());
		// }
		// });
		//
		// layout.addView(mCheckBox, new LinearLayout.LayoutParams(
		// LinearLayout.LayoutParams.MATCH_PARENT,
		// LinearLayout.LayoutParams.WRAP_CONTENT));
		//
		setContentView(layout);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
			new AlertDialog.Builder(this).setTitle(R.string.exit).setMessage(R.string.exit_msg).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			}).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {

				}
			}).show();
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	private class GalleryViewItem extends TableLayout {
		private EditText mEdit1;
		private TextView mText1;
		private TextView mText2;
		private Button mButton1;
		private Button mButton2;

		public GalleryViewItem(Context context, int position) {
			super(context);

			this.setOrientation(LinearLayout.VERTICAL);

			this.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

			mEdit1 = new EditText(context);

			this.addView(mEdit1, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

			mText1 = new TextView(context);
			mText1.setText(mLabelArray[position]);
			mText1.setTextSize(30);
			mText1.setGravity(Gravity.LEFT);
			mText1.setBackgroundColor(mColorArray[position]);

			this.addView(mText1, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

			mButton1 = new Button(context);
			mButton1.setText("<<");
			mButton1.setGravity(Gravity.LEFT);
			mButton1.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View view) {
					mGallery.movePrevious();
				}
			});

			this.addView(mButton1, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

			mButton2 = new Button(context);
			mButton2.setText(">>");
			mButton2.setGravity(Gravity.RIGHT);
			mButton2.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View view) {
					mGallery.moveNext();
				}
			});

			this.addView(mButton2, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

			mText2 = new TextView(context);
			mText2.setText(mLabelArray[position]);
			mText2.setTextSize(30);
			mText2.setGravity(Gravity.RIGHT);
			mText2.setBackgroundColor(mColorArray[position]);

			this.addView(mText2, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1));
		}
	}
}
